package com.xiaotu.spider.maoyan.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xiaotu.common.redis.RedisClientTemplate;
import com.xiaotu.common.util.GsonUtils;

import net.sf.json.JSONObject;
/***
 * 猫眼电影数据源
 * @author Administrator
 *
 */
public class MaoYanDataSource {
	
	public static List<JSONObject> jsonList = new ArrayList<JSONObject>();
	
	static {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-context.xml");
		RedisClientTemplate redisClient = context.getBean("redisClientTemplate", RedisClientTemplate.class);
		Map<String, String> targetMap = redisClient.hgetAll("MAOYAN_SEARCH_MOVIE_BAK");
		List<String> targetList = new ArrayList<String>(targetMap.values());
		int i = 0;
        for (String string : targetList)
        {
            if (StringUtils.isEmpty(string))
                continue;
            try
            {
                JSONObject jsonObj = GsonUtils.fromJson(string,
                        JSONObject.class);
                jsonList.add(0, jsonObj);
            }
            catch (Exception e)
            {
            	
            }
            i++;
        }
        
        redisClient.disconnect();
        
        //排序
        Collections.sort(jsonList, new Comparator<JSONObject>() {

			@Override
			public int compare(JSONObject o1, JSONObject o2) {
				if((o1.getInt("dataid")-o2.getInt("dataid"))>0) {
					return 1;
				}
				else if((o1.getInt("dataid")-o2.getInt("dataid"))<0) {
					return -1;
				}
				return 0;
			}
        	
		});
        //包头不包尾
        //jsonList = jsonList.subList(0, 500);//已完成
        //jsonList = jsonList.subList(500, 1000);//已完成
        //jsonList = jsonList.subList(1000, 1500);//已完成
        //jsonList = jsonList.subList(1500, 2000);//已完成
        //jsonList = jsonList.subList(2000, 2500);//已完成
        //jsonList = jsonList.subList(2500, 3000);//已完成
        jsonList = jsonList.subList(3000, jsonList.size());//已完成
        
	}
	
	public static void main(String[] args) {
		
		for(int i =0 ;i<jsonList.size();i++) {
			
			JSONObject jsonObject0 = jsonList.get(i);
			
			System.out.println(jsonObject0);
		}
		
	}
	
}
